#pragma once
// -------------------------------------------------
// BlinKit - BlinKit Library
// -------------------------------------------------
//   File Name: popup_menu_impl.h
// Description: PopupMenuImpl Class
//      Author: Ziming Li
//     Created: 2022-04-26
// -------------------------------------------------
// Copyright (C) 2022 MingYang Software Technology.
// -------------------------------------------------

// Copyright (c) 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef PopupMenuImpl_h
#define PopupMenuImpl_h

#include "blinkit/blink/renderer/core/page/page_popup_client.h"
#include "blinkit/blink/renderer/platform/popup_menu.h"

namespace blink {

class ChromeClientImpl;
class PagePopup;
class HTMLElement;
class HTMLHRElement;
class HTMLOptGroupElement;
class HTMLOptionElement;
class HTMLSelectElement;

class PopupMenuImpl final : public PopupMenu, public PagePopupClient
{
public:
    static GCRefPtr<PopupMenuImpl> create(ChromeClientImpl*, HTMLSelectElement&);
    ~PopupMenuImpl() override;

    void update();

    void dispose();

private:
    PopupMenuImpl(ChromeClientImpl*, HTMLSelectElement&);

    class ItemIterationContext;
    void addOption(ItemIterationContext&, HTMLOptionElement&);
    void addOptGroup(ItemIterationContext&, HTMLOptGroupElement&);
    void addSeparator(ItemIterationContext&, HTMLHRElement&);
    void addElementStyle(ItemIterationContext&, HTMLElement&);

    // PopupMenu functions:
    void show(const FloatQuad& controlPosition, const IntSize& controlSize, int index) override;
    void hide() override;
    void disconnectClient() override;
    void updateFromElement() override;

    // PagePopupClient functions:
    void writeDocument(SharedBuffer*) override;
    void selectFontsFromOwnerDocument(Document&) override;
    void setValueAndClosePopup(int, const String&) override;
    void setValue(const String&) override;
    void closePopup() override;
    Element& ownerElement() override;
    Locale& locale() override;
    void didClosePopup() override;

    ChromeClientImpl *m_chromeClient;
    HTMLSelectElement *m_ownerElement;
    PagePopup *m_popup = nullptr;
    bool m_needsUpdate = false;
};

}

#endif // PopupMenuImpl_h
